var vaMonth = "";
var vaYear = "";
$(document).ready(function(){
	
	loadZone();
	
	$("#inputWater").keypress(function (event) {
		if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
	       return false;
	    }
	});
	
	$("select[name=zone]#inputZone").change(function() {
		loadRouteStart();
		loadRouteEnd();
	});
});

function loadZone() {
	$.ajax({
		url : 'CS36002Srvl' , 
		type : "POST",
		data : {
			process_type: "getZone"
		},
	//	anync:false,
		dataType:"JSON",
		success: function(data){
			var selected="";
			var options = '';
			options += '<option value="">-- เลือกเขต --</option>';
			for (var i = 0; i < data.values.length; i++) {
				options += '<option value="' + data.values[i] + '" '+selected+'>' + data.messages[i] +  '</option>';
			}
			$("select[name=zone]#inputZone").html(options);
			var thMouth = ["มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม"];
			vaMonth = data.vaMonth;
			vaYear = data.vaYear;
			$("#vaMonthYear").html(thMouth[(vaMonth-1)] + ' ' + data.vaYear);
		}
	});
}

function loadRouteStart(zoneCode) {
	var vaZoneCode = $( "#inputZone :selected").val();
	if(zoneCode!=""){
		vaZoneCode = zoneCode;
	}
	if(isNaN(vaZoneCode)==true){
		vaZoneCode = document.getElementById("inputZone").value;
	}
	$.ajax({
		url : 'CS36002Srvl' , 
		type : "POST",
		data : {
			process_type: "getRoute" , 
			zoneCode: vaZoneCode
		},
	//	anync:false,
		dataType:"JSON",
		success: function(data){
			var options = '';
			options += '<option value="">-- เลือกเส้นทาง --</option>';
			var selected ="";
			for (var i = 0; i < data.values.length; i++) {
				options += '<option value="' + data.values[i] + '" '+selected+'>' + data.messages[i] +  '</option>';
			}
			$("select#inputRouteStart").html(options);
		}
	});
}

function loadRouteEnd(zoneCode) {
	var vaZoneCode = $( "#inputZone :selected").val();
	if(zoneCode!=""){
		vaZoneCode = zoneCode;
	}
	if(isNaN(vaZoneCode)==true){
		vaZoneCode = document.getElementById("inputZone").value;
	}
	$.ajax({
		url : 'CS36002Srvl' , 
		type : "POST",
		data : {
			process_type: "getRoute" , 
			zoneCode: vaZoneCode
		},
	//	anync:false,
		dataType:"JSON",
		success: function(data){
			var options = '';
			options += '<option value="">-- เลือกเส้นทาง --</option>';
			var selected ="";
			for (var i = 0; i < data.values.length; i++) {
				options += '<option value="' + data.values[i] + '" '+selected+'>' + data.messages[i] +  '</option>';
			}
			$("select#inputRouteEnd").html(options);
		}
	});
}

function printReport() {
	var vaZone = $("#inputZone").val();
	var vaRouteStart = $("#inputRouteStart").val();
	var vaRouteEnd = $("#inputRouteEnd").val();
	var vaWater = $("#inputWater").val();
	var vnSiteId = $("#siteId").val();
	
	if(vaZone.trim().length == 0 || vaRouteStart.trim().length == 0 || vaRouteEnd.trim().length == 0 
			|| vaWater.trim().length == 0)
	{

		$("#modal_content").html("กรุณากรอกข้อมูลให้ครบถ้วน");
		$("#warningModal").modal();
		return;
	}else{

		var url = reporturl+"CS36003R.jsp?zoneId="+vaZone+"&siteId="+vnSiteId+"&startRouteId="+vaRouteStart+"&endRouteId="+vaRouteEnd+"&periodMonth="+vaMonth+"&periodYear="+vaYear+"&percent="+vaWater;
		window.open(url);

	}
}